#-*- conding: encoding -*-
#!/usr/bin/python3
import baostock as bs
from datetime import date, timedelta, datetime

# 获取这个月到今天日期
def days_in_cur_month():
    m = datetime.now().month
    y = datetime.now().year
    ndays = datetime.now().day
    d1 = date(y, m, 1)
    d2 = date(y, m, ndays)
    delta = d2 - d1
    return [(d1 + timedelta(days=i)).strftime('%Y-%m-%d') for i in range(delta.days + 1)]

# 获取日期范围内的股票交易日
def get_trade_dates():
    days = days_in_cur_month()
    if (len(days) == 0):
        print("这个月没有可交易日期, 程序结束")
        return []
    # lg = bs.login()
    rs = bs.query_trade_dates(start_date=days[0], end_date=days[len(days) - 1])
    print('query_trade_dates respond error_code:'+rs.error_code)
    print('query_trade_dates respond error_msg:'+rs.error_msg)
    # bs.logout()
    #### 打印结果集 ####
    data_list = []
    while (rs.error_code == '0') & rs.next():
        data_list.append(rs.get_row_data())
    trade_date_list = []
    for x in data_list:
        if (x[1] == "1"):
            trade_date_list.append(x[0])

    return trade_date_list